home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / c / RConfig.lha / RConfig_v1.1 / src / stkchker.c < prev   
Encoding:
C/C++ Source or Header  |  1992-09-10  |  5.0 KB  |  158 lines

  1. /*
  2.  * RConfig -- Replacement Library Configuration
  3.  *   Copyright 1991, 1992 by Anthon Pang, Omni Communications Products
  4.  *
  5.  * Source File: stkchker.c
  6.  * Description: Modify Manx cc 5.2a generated assembler (.asm) files, to
  7.  *   support my version of _stkchk() and RConfig code.
  8.  * Comments: formerly stkchk.rexx 1.2 (92.08.30)
  9.  * Usage: stkchker filename.asm
  10.  *    or: stkchker filename.a68
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <fcntl.h>
  15. #include <string.h>
  16.  
  17. static char *verstag = "\0$VER: stkchker 1.3 (30.08.92)";
  18.  
  19. #define BUFFERSIZE 1024
  20.  
  21. #define filename    l1
  22. #define oldname     l2
  23. #define stackframesize  tmp
  24.  
  25. char *l,*l1,*l2;    /* line buffers */
  26.  
  27. #define exists(f)       (access((char*)f, 0)==0)
  28. #define system(c)       Execute((STRPTR)c, 0L, 0L)
  29. #define swap(x,y)       tmp=x;x=y;y=tmp
  30.  
  31. static char *STKCHKSTRING = "\x09" "jsr" "\x09" "__stkchk#\n";
  32. static char *MOVEWSTRING = "\x09" "move.w" "\x09";
  33.  
  34. void writeln(FILE *f, char *buffer) {
  35.     fwrite(buffer, strlen(buffer), 1, f);
  36. }
  37.  
  38. /* appends \n if present */
  39. void readln(FILE *f, char *buffer) {
  40.     char c;
  41.     int l;
  42.  
  43.     l = 0;
  44.     while ((c=fgetc(f))!=EOF) {
  45.         *buffer++ = c;
  46.         l++;
  47.         if (l == BUFFERSIZE)
  48.             exit(20);
  49.         if (c == '\n')
  50.             break;
  51.     }
  52.     *buffer = '\0';
  53. }
  54.  
  55. int main(int argc, char *argv[]) {
  56.     int n;
  57.     FILE *infile;
  58.     FILE *outfile;
  59.     char *tmp;
  60.  
  61.     l = (char*)malloc(BUFFERSIZE);
  62.     l1 = (char*)malloc(BUFFERSIZE);
  63.     l2 = (char*)malloc(BUFFERSIZE);
  64.  
  65.     if (!l || !l1 || !l2)
  66.         exit(20);
  67.  
  68.     n = argc;
  69.  
  70.     if (n == 2) {
  71.         strcpy(filename, argv[1]);
  72.  
  73.         /* guesses if file doesn't exist...possibly omitted extension? */
  74.         if (!exists(filename)) {
  75.             strcat(filename, ".asm");
  76.         }
  77.         if (!exists(filename)) {
  78.             strcpy(filename, argv[1]);
  79.             strcat(filename, ".a68");
  80.         }
  81.  
  82.         if (exists(filename)) {
  83.  
  84.             strcpy(oldname, filename);
  85.             strcat(oldname, "~");
  86.  
  87.             sprintf(l, "copy %s %s", filename, oldname);
  88.             system(l);            
  89.  
  90.             if (infile = fopen(oldname, "r")) {
  91.                 if (outfile = fopen(filename, "w")) {
  92.                     readln(infile, l);
  93.  
  94.                     while (!feof(infile)) {
  95.                         if (strncmp(l+1, "link", 4)==0) {
  96.                             swap(l1,l);
  97.                             readln(infile, l);
  98.  
  99.                             if (strncmp(l+1, "movem.l", 7)==0) {
  100.                                 swap(l2,l);
  101.                                 readln(infile, l);
  102.  
  103.                                 if (strcmp(l, STKCHKSTRING)==0) {
  104.                                     stackframesize = strrchr(l1, ',')+1;
  105.                                     writeln(outfile, MOVEWSTRING);
  106.                                     fwrite(stackframesize, strlen(stackframesize)-1, 1, outfile); /* ignore \n */
  107.                                     writeln(outfile, ",d0\n");
  108.  
  109.                                     writeln(outfile, l);
  110.                                     writeln(outfile, l1);
  111.                                     writeln(outfile, l2);
  112.                                 } else {
  113.                                     /* hmmm...not a 3 line stkchk, so just copy lines */
  114.                                     writeln(outfile, l1);
  115.                                     writeln(outfile, l2);
  116.                                     writeln(outfile, l);
  117.                                 }
  118.                             } else if (strcmp(l, STKCHKSTRING)==0) {
  119.                                 stackframesize = strrchr(l1, ',')+1;
  120.                                 writeln(outfile, MOVEWSTRING);
  121.                                 fwrite(stackframesize, strlen(stackframesize)-1, 1, outfile); /* ignore \n */
  122.                                 writeln(outfile, ",d0\n");
  123.                                 writeln(outfile, l);
  124.                                 writeln(outfile, l1);
  125.                             } else {
  126.                               /* hmmm...not a two line stkchk, so just copy lines */
  127.                               writeln(outfile, l1);
  128.                               writeln(outfile, l);
  129.                             }
  130.                         } else {
  131.                             /* not a local stack frame */
  132.                             if (strcmp(l, STKCHKSTRING)==0) {
  133.                                 writeln(outfile, MOVEWSTRING);
  134.                                 writeln(outfile, "0,d0\n");
  135.                             }
  136.                             writeln(outfile, l);
  137.                         }
  138.                         readln(infile, l);
  139.                     }
  140.                     fclose(outfile);
  141.                     puts("Done.");
  142.                 } else {
  143.                     puts("Unable to write file.");
  144.                 }
  145.                 fclose(infile);
  146.             } else {
  147.                 puts("Unable to read file.");
  148.             }
  149.         } else {
  150.             printf("File: %s doesn't exist.\n", argv[1]);
  151.         }
  152.     } else if (argc) {
  153.         printf("Usage: %s filename.asm\n", argv[0]);
  154.     }
  155.  
  156.     return 0;
  157. }
  158.